package com.msmall.community.team.dao.hiberante;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.team.dao.TeamDao;
import com.msmall.community.team.model.Team;

public class TeamDaoHiberante extends BaseDaoHibernate<Team,Long> implements TeamDao{
	
	public TeamDaoHiberante(){
		super(Team.class);
	}
	
	/**
	 * 通过组名查找组；精确查找
	 */
	public Team loadTeamByName(String name) throws Exception{
		try{
			String queryString = "from Team where name=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{name});
			if(list == null || list.size() == 0)
				return null;
			return (Team) list.get(0);
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 通过用户ID和职位，查找到此用户所在组
	 * eg : postion = TeamUser.Position_chairman用户所有创建的组
	 * @param userid
	 */
	public List<Team> loadTeamsByUserPosition(Long userid, Integer position) throws Exception{
		try{
			String queryString = "select t from Team t, TeamUser u where t.teamid=u.teamid and u.position=? and u.userid=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{position, userid});
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 通过用户ID，查找到此用户所在组
	 * @param userid
	 */
	public List<Team> loadTeamsByUserStatus(Long userid, Integer status) throws Exception{
		try{
			String queryString = "select t from Team t, TeamUser u where t.teamid=u.teamid and u.userid=? and t.status=?";
			List list = getHibernateTemplate().find(queryString, new Object[]{userid, status});
			return list;
		}catch(Exception e){
			throw e;
		}
	}
}
